let cess = {};
var vm = new Vue({
  el: "#app",
  data: {
    isLoading: false,
    addr: "cXfnLrW67qKkTn7DPXfh1SykwLSmqzrui2D81RVooUSV4e5VK",
    balance: '--',
  },
  beforeMount: function () {
    const that = this;
  },
  mounted: function () { },
  methods: {
    loading(isShow) {
      this.isLoading = isShow;
    },
    async onConnectRPC() {
      if (!window.cessSdk) {
        return this.$alert('window.cessSdk not found');
      }
      this.loading(true);
      console.log(window);
      const tmp = await window.cessSdk.InitAPI();
      cess.api = tmp.api;
      cess.keyring = tmp.keyring;
      cess.common = new window.cessSdk.Common(tmp.api, tmp.keyring);
      cess.space = new window.cessSdk.Space(tmp.api, tmp.keyring);
      cess.authorize = new window.cessSdk.Authorize(tmp.api, tmp.keyring);
      cess.bucket = new window.cessSdk.Common(tmp.api, tmp.keyring);
      cess.file = new window.cessSdk.Common(tmp.api, tmp.keyring);
      this.sub(this.addr);
      this.loading(false);
    },
    async sub(addr) {
      let that = this;
      await cess.api.query.system.account(
        addr,
        (info) => that.balance = formatAmount(info.data.free.toBigInt()))
    },
    async getAccountsFromWallet(){
      let list = await cess.authorize.getAccountsFromWallet();
      console.log({ list });
    },
    async onAuthorizeList() {
      let list = await cess.authorize.authorityList(this.addr);
      console.log({ list });
    },
    async onAuthorize() {
      let list = await cess.authorize.authorize(this.addr, window.cessSdk.defaultConfig.gatewayAddr);
      console.log({ list });
    },
    async onCancelAuthorize() {
      let list = cess.authorize.cancelAuthorize(this.addr, window.cessSdk.defaultConfig.gatewayAddr);
      console.log({ list });
    }
  },
});
